import {
    request
} from "@/utils/request"
const {
    toUrl,
    globalData
} = getApp()

// pages/store/shopCart/list.js
Page({
    toUrl,
    /**
     * 页面的初始数据
     */
    data: {
        quota_num:0,
        globalData,
        suppliers_id: -1,
        detail: [],
        dialogVisble: false,
        currentDelForm: {
            type: null,
            id: null
        },
        goods: [],
        selImg: '/image/car10.png',
        isTrue: false,
    },
    //获取购物车
    getList() {
        const that = this
        const data = {
            suppliers_id: this.data.suppliers_id
        }
        return request('cart/cartlist', {
            data
        }).then(res => {
            that.setData({
                detail: res.data,
                goods: res.data.goods
            })
            let selnum = 0;
            that.data.goods.forEach(item => {
                if (item.selected == 1) {
                    selnum += 1
                }
            })
            if (selnum < that.data.goods.length) {
                that.setData({
                    selImg: ''
                })
            } else {
                that.setData({
                    selImg: '/image/car10.png'
                })
            }
        })
    },
    //购买--跳转提交订单
    sell() {
        let ids = null
        this.data.goods.forEach(item => {
            if (item.selected == 1) {
                if (!ids) {
                    ids = item.goods_id
                } else {
                    ids = ids + ',' + item.goods_id
                }
            }
        })
        wx.navigateTo({
            url: '/pages/order/form?type=cart&suppliers_id=' + this.data.detail.store.suppliers_id + '&ids=' + ids + '&quota_num=' + this.data.quota_num,
        })
    },
    //更新购物车数量
    updateCartNum(ids, num) {
        const data = {
            typeid: 1,
            ids: ids,
            num: num
        }
        return request('cart/setcart', {
            data
        })
    },
    //更新购物车选中
    updateCartSel(e) {
        // const detail = this.data.detail
        const goods = this.data.goods
        const index = e.currentTarget.dataset.index
        let ids = ''
        goods[index].selected = goods[index].selected == 1 ? 0 : 1
        goods.forEach(item => {
            if (item.selected == 1) {
                if (!ids) {
                    ids = item.id
                } else {
                    ids = ids + ',' + item.id
                }
            }
        })
        const data = {
            ids: ids,
            suppliers_id: this.data.suppliers_id
        }
        request('cart/setcart', {
            data
        }).then(res => {
            if (res.errcode == 1) {
                this.getList()
            }
        })
    },
    //全选
    updateAll() {
        const goods = this.data.goods
        const len = goods.length
        let selNum = 0
        goods.forEach(item => {
            if (item.selected == 1) {
                selNum += 1
            }
        })
        if (selNum != 0) {
            //选中数量和商品数量相同，取消选中
            if (len == selNum) {
                const data = {
                    typeid: 2,
                    ids: '',
                    suppliers_id: this.data.suppliers_id
                }
                request('cart/setcart', {
                    data
                }).then(res => {
                    if (res.errcode == 1) {
                        this.getList()
                    }
                })
            } else if (selNum < len) {
                //部分选中，全选
                let ids = ''
                goods.forEach(item => {
                    if (!ids) {
                        ids = item.id
                    } else {
                        ids = ids + ',' + item.id
                    }
                })
                const data = {
                    typeid: 2,
                    ids: ids,
                    suppliers_id: this.data.suppliers_id
                }
                request('cart/setcart', {
                    data
                }).then(res => {
                    if (res.errcode == 1) {
                        this.getList()
                    }
                })
            }
        } else {
            //为0的时候说明购物车没有商品，或者全都没选中
            if (len == 0) {
                return
            } else {
                //全选
                let ids = ''
                goods.forEach(item => {
                    if (!ids) {
                        ids = item.id
                    } else {
                        ids = ids + ',' + item.id
                    }
                })
                const data = {
                    typeid: 2,
                    ids: ids,
                    suppliers_id: this.data.suppliers_id
                }
                request('cart/setcart', {
                    data
                }).then(res => {
                    if (res.errcode == 1) {
                        this.getList()
                    }
                })
            }
        }
    },
    //增加数量
    plus(e) {
        // const detail = this.data.detail

        if (this.data.quota_num == this.data.goods[0]?.num) {
            wx.showToast({
              title: `限购${this.data.quota_num}个`,
              icon:'none'
            })
            return
        }

        if (this.data.isTrue == true) {
            return
        }
        this.setData({
            isTrue: true

        });
        setTimeout(() => {

            this.setData({
                isTrue: false
            })
        }, 1000);
        const index = e.currentTarget.dataset.index
        const goods = this.data.goods
        goods[index].num += 1
        const ids = goods[index].id
        const num = goods[index].num
        this.updateCartNum(ids, num).then(res => {
            if (res.errcode == 1) {
                this.getList()
            }
        })
    },
    //减少数量
    del(e) {
        // const detail = this.data.detail
        if (this.data.isTrue == true) {
            return
        }
        this.setData({
            isTrue: true

        });
        setTimeout(() => {

            this.setData({
                isTrue: false
            })
        }, 1000);
        const index = e.currentTarget.dataset.index
        const goods = this.data.goods
        if (goods[index].num == 1) {
            return
        }
        goods[index].num -= 1
        const ids = goods[index].id
        const num = goods[index].num
        this.updateCartNum(ids, num).then(res => {
            if (res.errcode == 1) {
                this.getList()
            }
        })
    },
    //点击删除
    clickDel(e) {

        const {
            type
        } = e.currentTarget.dataset
        if (this.data.selImg == '' && type == 2) {
            return
        }
        const currentDelForm = this.data.currentDelForm
        currentDelForm.type = type
        if (type == 1) {
            const {
                id
            } = e.currentTarget.dataset
            currentDelForm.id = id
        }
        this.setData({
            dialogVisble: !this.data.dialogVisble,
            currentDelForm: currentDelForm
        })
    },
    //删除按钮
    delCart(e) {
        const {
            type
        } = this.data.currentDelForm
        if (type == 1) {
            const {
                id
            } = this.data.currentDelForm
            this.deleteSingleGoods(id)
        } else if (type == 2) {
            this.deleteAllGoods()
        }
    },
    //购物车删除
    deleteSingleGoods(ids) {
        const id = ids
        const that = this
        const data = {
            ids: id
        }
        request('cart/delcart', {
            data
        }).then(res => {
            if (res.errcode == 1) {
                wx.showToast({
                    title: res.message,
                })
                that.setData({
                    dialogVisble: false
                })
                this.getList()
            }
        })
    },
    //购物车全部删除
    deleteAllGoods() {
        const that = this
        const list = this.data.goods
        let goodsStr = ''
        list.forEach(item => {
            if (!goodsStr) {
                goodsStr = item.id
            } else {
                goodsStr = goodsStr + ',' + item.id
            }
            const data = {
                ids: goodsStr
            }
            request('cart/delcart', {
                data
            }).then(res => {
                if (res.errcode == 1) {
                    wx.showToast({
                        title: res.message,
                    })
                    that.setData({
                        dialogVisble: false
                    })
                    this.getList()
                }
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        wx.showLoading({
            title: '加载中'
        })
        const {
            suppliers_id,
            quota_num
        } = options
        this.setData({
            suppliers_id: suppliers_id,
            quota_num:quota_num
        })
        this.getList().then(() => {

            setTimeout(() => {
                wx.hideLoading()
            }, 500);
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})